//
//  ZhangDanMingXiViewController.m
//  EJLX
//
//  Created by adc on 15/11/16.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "ZhangDanMingXiViewController.h"
#import "ZhangDanTableViewCell.h"
#import "SecondZhangDanMingXi.h"
@interface ZhangDanMingXiViewController ()

@end

@implementation ZhangDanMingXiViewController
-(void)viewWillAppear:(BOOL)animated
{
    [HttpsManager requestZhangDanMingXi:@"1" completion:^(ZhangDanMingXi *mingXi)
     {
         if (mingXi!=nil)
         {
             _mingXi=mingXi;
             _dataAry=_mingXi.listAry;
             __weak ZhangDanMingXiViewController *weakSelf = self;
             [tabeiView addPullToRefreshWithActionHandler:^{
                 NSLog(@"当进行下拉刷新执行该方法");
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
             
             [tabeiView addInfiniteScrollingWithActionHandler:^{
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];

             [tabeiView reloadData];
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
        
     }];
}
- (void)requestEnd{
    [tabeiView.pullToRefreshView stopAnimating];
    
    [tabeiView.infiniteScrollingView stopAnimating];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-35, 10, 70, 24)];
    label.text=@"账单明细";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    for (int i=0;i<3;i++)
    {
        UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake(0+WIDE/3*i, 64,WIDE/3, 46)];
        but.tag=i+1;
        NSArray*ary=[NSArray arrayWithObjects:@"全部",@"已收入",@"提现流水", nil];
        [but setTitle:ary[i] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:but];
        
    }
    label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 64+46,WIDE/3,2)];
    label1.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:label1];
    tabeiView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64+46+5+5, WIDE,HEIGHT-64-46-5-5) style:UITableViewStylePlain];
    tabeiView.delegate=self;
    tabeiView.dataSource=self;
    tabeiView.rowHeight=70;
    tabeiView.tableFooterView=[[UIView alloc]init];
    tabeiView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tabeiView];
    [tabeiView registerNib:[UINib nibWithNibName:@"ZhangDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return _dataAry.count;
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondZhangDanMingXi*sec=_dataAry[indexPath.row];
    ZhangDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.CTLabel.text=sec.changetype;
    cell.MonyLabel.text=[NSString stringWithFormat:@"%.2f",sec.changeAmount*0.01];
    cell.RiQiLabel.text=sec.billingtime;
    cell.JiaoYiLabel.text=sec.billingtype;
    return cell;
}
//返回按钮
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)butClick:(UIButton*)sender
{
    [HttpsManager requestZhangDanMingXi:[NSString stringWithFormat:@"%ld",(long)sender.tag] completion:^(ZhangDanMingXi *mingXi)
    {
        if (mingXi!=nil)
        {
            _mingXi=mingXi;
            _dataAry=_mingXi.listAry;
            [tabeiView reloadData];
        }
        else
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }

    }];
    
    [UIView animateWithDuration:1 animations:^{
        label1.frame=CGRectMake(WIDE/3*(sender.tag-1), 64+46,WIDE/3,2);
    } completion:^(BOOL finished)
     {
         nil;
     }];
    
}


    // Do any additional setup after loading the view from its nib.


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
